<template>
    <div class="app-headers" :class="{ 'stickys': isSticky }">
        <div class="container">
            <!-- logo -->
            <h1 class="logo">
                <router-link to="/"></router-link>
            </h1>
            <!-- 导航区域 -->
            <ul class="app-header-nav">
                <li class="home"><router-link to="/">首页</router-link></li>
                <li class="hh" v-for="item in list" :key="item.id" @mouseover="$set(submenuOpenStatus, item.id, true)"
                    @mouseleave="$set(submenuOpenStatus, item.id, false)">
                    <router-link to="/">{{ item.name }}</router-link>
                    <div :class="{ 'layer': true, 'open': submenuOpenStatus[item.id] }">
                        <ul class="aiai">
                            <li v-for="i in item.children" :key="i.id">
                                <router-link to="/">
                                <img :src="i.picture" alt="">
                                <p>{{ i.name }}</p>
                                </router-link>
                            </li>
                        </ul>
                    </div>
                </li>
            </ul>
                  <!--右侧 竖线 品牌和专题  -->
            <div class="right">
              <span class="fl">|</span>
              <li class="fl pp"><router-link to="/">品牌</router-link></li>
              <li class="fl"><router-link to="/">专题</router-link></li>
            </div>
        </div>
    </div>
</template>

<script>
  import { getCategory } from '@/request/api/myc/myc';
  
  export default {
    data() {
      return {
        list:[],
        isSticky: false,
        submenuOpenStatus: {}
      };
    },
    mounted() {
      this.Category(); 

      window.addEventListener('scroll', this.handleScroll);

              // 初始化submenuOpenStatus对象
        this.list.forEach(item => {
            this.$set(this.submenuOpenStatus, item.id, false);
        });
    },
    methods: {
        // 引入接口
      async Category() {
        const res = await getCategory();
        console.log("新鲜好物", res.data.result);
        this.list=res.data.result
      },
      handleScroll() {
        if (window.scrollY > 80) {
          this.isSticky = true;
          console.log(1);
        } else {
          this.isSticky = false;
          console.log(2);
        }
    }
  }
};
</script>

<style lang="scss" scoped>
.app-headers{
    width: 100%;
    height: 80px;
    position: fixed;
    left: 0;
    top: 0;
    background: white;
    border-bottom: 1px dotted #ccc;
    z-index: 500;
    //   利用 c3的平移 平移了自身的高度+完全透明 隐藏
    transform: translateY(-100%);
    opacity: 0;
    //当滚动条向下滑动80px 给该元素添加类名show 没有到达80px时不添加类名show
    &.stickys{
        // height: 80px;
        // line-height: 80px;
        transition: all 0.3s linear; //全选  慢速
        transform: none;
        opacity: 1;
    }
}

.container {
  display: flex;
  align-items: center;
  .logo {
    width: 200px;
    height: 80px;
    background-image: url("../assets/images/logo.png");
    background-size: 160px;
    background-repeat: no-repeat;
  }
  .app-header-nav {
        display: flex;
        width: 820px;
        justify-content: space-evenly;

        .home {
            a {

                // 对于频繁使用的东西 都可以定义变量
                &:hover {
                    color: #27ba9b;
                    border-bottom: 1px solid #27ba9b;
                }
            }
        }

        .hh {
            a {

                // 对于频繁使用的东西 都可以定义变量
                &:hover {
                    color: #27ba9b;
                    border-bottom: 1px solid #27ba9b;
                }

            }

            .layer {
                width: 1240px;
                background-color: #fff;
                position: absolute;
                left: 0;
                top: 80px;
                height: 0;
                overflow: hidden;
                opacity: 0;
                box-shadow: 0 0 5px #ccc;
                transition: all .2s .1s;
                z-index: 500;
                .aiai {
                    display: flex;
                    flex-wrap: wrap;
                    padding: 0 70px;
                    align-items: center;
                    height: 132px;

                    li {
                        width: 110px;
                        text-align: center;

                        a {


                            img {
                                width: 60px;
                                height: 60px;
                            }

                            p {
                                padding-top: 10px;
                            }
                        }
                    }
                }
            }

            .layer.open {
                height: 132px;
                opacity: 1;
            }
        }
    }
  .right{
    span{
        color:#27ba9b;
        margin-right: 45px;
    }
    li{
        list-style: none;
        margin-right: 45px;
        border-bottom:1px solid transparent;
        a{
            &:hover {
              color: #27ba9b;
              border-bottom: 1px solid #27ba9b;
            }
        }
    }
}
}
</style>